//
//  KABillDetailSectionHeader.swift
//  kano
//
//  Created by Frank on 2024/9/18.
//

import Foundation
import UIKit

class KABillDetailSectionHeader: UITableViewHeaderFooterView {
    
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(13), weight: .bold)
        label.textColor = .color323135
        label.text = ""
        return label
    }()
    lazy var var_arrow: UIImageView = {
        let var_arrow = UIImageView(image: UIImage(named: "common_arrow_down"))
        return var_arrow
    }()
    lazy var detailTitle: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = UIFont.systemFont(ofSize: kscale(14), weight: .regular)
        label.textColor = .colorAEACB3
        label.text = ""
        return label
    }()
    
    var var_handler: (() -> ())?
    
    var open: Bool {
        get {
            return true
        }
        set {
            var_arrow.image = newValue ? UIImage(named: "common_arrow_down") : UIImage(named: "common_arrow_up")
        }
    }
    
    var topCorner: Bool = false
    var bottomCorner: Bool = false
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .white
        let var_tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(_ :)))
        contentView.addGestureRecognizer(var_tap)
        ka_setUI()
    }
    
    @objc func tapClick(_ sender: UITapGestureRecognizer) {
        if (var_handler != nil) {
            var_handler!()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ka_setUI() {
        backgroundColor = .white
        contentView.addSubview(var_arrow)
        contentView.addSubview(titleLabel)
        contentView.addSubview(detailTitle)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        var_arrow.snp.makeConstraints { make in
            make.size.equalTo(CGSizeMake(kscale(16), kscale(16)))
            make.right.equalTo(contentView).offset(kscale(-12));
            make.centerY.equalTo(contentView)
        }
        detailTitle.snp.makeConstraints { make in
            make.right.equalTo(var_arrow.snp.left).offset(kscale(-6))
            make.centerY.equalTo(var_arrow)
        }
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(kscale(16))
            make.centerY.equalTo(contentView)
        }
        
        if (topCorner && bottomCorner) {
            contentView.ka_addCorner(withRadius: kscale(10))
        } else {
            if (topCorner) {
                contentView.ka_addTopCorner(withRadius: kscale(10))
            }
            if (bottomCorner) {
                contentView.ka_addBottomCorner(withRadius: kscale(10))
            }
        }
    }
    
}
